package com.ran.java_gobang_new.config;

import com.ran.java_gobang_new.api.GameAPI;
import com.ran.java_gobang_new.api.MarchAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ran
 * Date: 2025-09-03
 * Time: 12:52
 */
@EnableWebSocket // 确定建立websocket连接
@Configuration // 作为组件注册到 spring中
// 把注册 websocket 注册进去
public class WebSocketConfig implements WebSocketConfigurer {

    @Autowired
    private MarchAPI marchAPI;
    @Autowired
    private GameAPI gameAPI;
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(marchAPI,"/match")
                .addInterceptors(new HttpSessionHandshakeInterceptor()) // 将 HttpSession 中的 Session 添加到 websocket 中
                .addHandler(gameAPI,"/game")
                .addInterceptors(new HttpSessionHandshakeInterceptor()); // 将 HttpSession 中的 Session 添加到 websocket 中
    }
}
